/**   
 *    
 * @file
 * @brief
 * @文件名	: NetStateChangeReceiver
 * @版权所有	: 网龙应用软件开发一部
 * @作  者	: chenqian
 * @创建时间	: 2014-7-15 下午3:09:56 
 * @文件描述	:  
 * @version	Ver 1.1
 */
package com.common.bean;

import com.common.commonInterface.INetStateChangeCallback;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 网络状态改变接收器 <br>
 * Created 2014-7-15 下午3:09:56
 * 
 * @version 1.2
 * @author chenqian
 * 
 * @see
 */

public class NetStateChangeReceiver extends BroadcastReceiver {
	INetStateChangeCallback mNetStateChangeCallback;

	/**
	 * Creates a new instance of NetStateChangeReceiver. <br>
	 * Created 2014-7-16 上午10:19:46
	 * 
	 * @param callback
	 *            INetStateChangeCallback
	 */
	public NetStateChangeReceiver(INetStateChangeCallback callback) {
		mNetStateChangeCallback = callback;
	}

	@Override
	public void onReceive(Context ctx, Intent intent) {
		if (null == mNetStateChangeCallback) {
			return;
		}
		String action = intent.getAction();
		if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
			ConnectivityManager connectivityManager = (ConnectivityManager) ctx
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = connectivityManager.getActiveNetworkInfo();
			if (info != null && info.isAvailable()) {
				NetworkInfo wifi = connectivityManager
						.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
				NetworkInfo mobile = connectivityManager
						.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
				if (null != wifi && wifi.isConnected()) {
					// 如果是wifi连上的操作
					mNetStateChangeCallback.afterWifiConnected();
				} else if (null != mobile && mobile.isConnected()) {
					// 如果是移动网络连上时的操作
					mNetStateChangeCallback.afterMobileConnected();
				} else {
					// 如果其他？
				}
			} else {
				// 没有可用网络
				mNetStateChangeCallback.netIsNotAvailable();
			}
		}
	}
}
